package com.coin.controller;

import javax.validation.Valid;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.coin.exchange.ok.vo.OkBatchTradeRequestVO;
import com.coin.exchange.ok.vo.OkCancelOrderRequestVO;
import com.coin.exchange.ok.vo.OkFutureCancelVO;
import com.coin.exchange.ok.vo.OkFutureDepthVO;
import com.coin.exchange.ok.vo.OkFutureOrderInfoVO;
import com.coin.exchange.ok.vo.OkFuturePositionVO;
import com.coin.exchange.ok.vo.OkFutureTickerVO;
import com.coin.exchange.ok.vo.OkFutureTradeVO;
import com.coin.exchange.ok.vo.OkFutureTradesVO;
import com.coin.exchange.ok.vo.OkFutureUserinfoVO;
import com.coin.exchange.ok.vo.OkOrderHistoryRequestVO;
import com.coin.exchange.ok.vo.OkOrderInfoRequestVO;
import com.coin.exchange.ok.vo.OkOrdersInfoRequestVO;
import com.coin.exchange.ok.vo.OkTradeRequestVO;
import com.coin.exchange.ok.vo.OkTradesRequestVO;
import com.coin.exchange.ok.vo.OkUserinfoRequestVO;
import com.coin.exchange.okcoin.rest.future.IFutureRestApi;
import com.coin.exchange.okcoin.rest.stock.IStockRestApi;
import com.coin.framework.bean.ResultForm;

import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/rest/okcoin")
public class OkcoinController {

	private static Logger logger = LoggerFactory.getLogger(OkcoinController.class);
	
	@Autowired
	private IStockRestApi iStockRestApi;

	@Autowired
	private IFutureRestApi iFutureRestApi;
	
	@ApiOperation(value = "行情")
	@RequestMapping(value = "/getTicker", method = RequestMethod.POST)
	public ResultForm<?> getTicker(@RequestBody String symbol) {
		ResultForm<?> resultForm = null;
//		String symbol = jsonObject.getString("symbol");
		try {
			String res = iStockRestApi.ticker(symbol);
			JSONObject resultObject = JSONObject.parseObject(res);
			resultForm = ResultForm.createSuccessResultForm(resultObject, "成功");
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage(), e);
			resultForm = ResultForm.createErrorResultForm(null, e.getMessage());
		}
		return resultForm;
	}

	@ApiOperation(value = "市场深度")
	@RequestMapping(value = "/getDepth", method = RequestMethod.POST)
	public ResultForm<?> getDepth(@RequestBody String symbol) {
		ResultForm<?> resultForm = null;
//		String symbol = jsonObject.getString("symbol");
		try {
			String res = iStockRestApi.depth(symbol);
			JSONObject resultObject = JSONObject.parseObject(res);
			resultForm = ResultForm.createSuccessResultForm(resultObject, "成功");
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage(), e);
			resultForm = ResultForm.createErrorResultForm(null, e.getMessage());
		}
		return resultForm;
	}

	@ApiOperation(value = "现货历史交易信息")
	@RequestMapping(value = "/getTrades", method = RequestMethod.POST)
	public ResultForm<?> getTrades(@RequestBody @Valid OkTradesRequestVO vo) {
		ResultForm<?> resultForm = null;
		String symbol = vo.getSymbol();
		String since = vo.getSince();
		try {
			String res = iStockRestApi.trades(symbol, since);
			JSONArray resultObject = JSONArray.parseArray(res);
			resultForm = ResultForm.createSuccessResultForm(resultObject, "成功");
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage(), e);
			resultForm = ResultForm.createErrorResultForm(null, e.getMessage());
		}
		return resultForm;
	}

	@ApiOperation(value = "获取用户信息")
	@RequestMapping(value = "/getUserinfo", method = RequestMethod.POST)
	public ResultForm<?> getUserinfo(@RequestBody @Valid OkUserinfoRequestVO vo) {
		ResultForm<?> resultForm = null;
		try {
			String api_key = vo.getApi_key();
			String secret_key = vo.getSecret_key();
			String res = iStockRestApi.userinfo(api_key, secret_key);
			JSONObject resultObject = JSONObject.parseObject(res);
			resultForm = ResultForm.createSuccessResultForm(resultObject, "成功");
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage(), e);
			resultForm = ResultForm.createErrorResultForm(null, e.getMessage());
		}
		return resultForm;
	}

	@ApiOperation(value = "下单交易")
	@RequestMapping(value = "/trade", method = RequestMethod.POST)
	public ResultForm<?> trade(@RequestBody @Valid OkTradeRequestVO vo) {
		ResultForm<?> resultForm = null;
		try {
			String api_key = vo.getApi_key();
			String secret_key = vo.getSecret_key();
			String symbol = vo.getSymbol();
			String type = vo.getType();
			String price = vo.getPrice();
			String amount = vo.getAmount();
			String res = iStockRestApi.trade(api_key, secret_key, symbol, type, price, amount);
			JSONObject resultObject = JSONObject.parseObject(res);
			resultForm = ResultForm.createSuccessResultForm(resultObject, "成功");
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage(), e);
			resultForm = ResultForm.createErrorResultForm(null, e.getMessage());
		}
		return resultForm;
	}

	@ApiOperation(value = "批量下单")
	@RequestMapping(value = "/batchTrade", method = RequestMethod.POST)
	public ResultForm<?> batchTrade(@RequestBody @Valid OkBatchTradeRequestVO vo) {
		ResultForm<?> resultForm = null;
		try {
			String api_key = vo.getApi_key();
			String secret_key = vo.getSecret_key();
			String symbol = vo.getSymbol();
			String type = vo.getType();
			String orders_data = vo.getOrders_data();
			String res = iStockRestApi.batch_trade(api_key, secret_key, symbol, type, orders_data);
			JSONObject resultObject = JSONObject.parseObject(res);
			resultForm = ResultForm.createSuccessResultForm(resultObject, "成功");
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage(), e);
			resultForm = ResultForm.createErrorResultForm(null, e.getMessage());
		}
		return resultForm;
	}

	@ApiOperation(value = "撤销订单")
	@RequestMapping(value = "/cancelOrder", method = RequestMethod.POST)
	public ResultForm<?> cancelOrder(@RequestBody @Valid OkCancelOrderRequestVO vo) {
		ResultForm<?> resultForm = null;
		try {
			String api_key = vo.getApi_key();
			String secret_key = vo.getSecret_key();
			String symbol = vo.getSymbol();
			String order_id = vo.getOrder_id();
			String res = iStockRestApi.cancel_order(api_key, secret_key, symbol, order_id);
			JSONObject resultObject = JSONObject.parseObject(res);
			resultForm = ResultForm.createSuccessResultForm(resultObject, "成功");
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage(), e);
			resultForm = ResultForm.createErrorResultForm(null, e.getMessage());
		}
		return resultForm;
	}

	@ApiOperation(value = "获取用户的订单信息")
	@RequestMapping(value = "/getOrderInfo", method = RequestMethod.POST)
	public ResultForm<?> getOrderInfo(@RequestBody @Valid OkOrderInfoRequestVO vo) {
		ResultForm<?> resultForm = null;
		try {
			String api_key = vo.getApi_key();
			String secret_key = vo.getSecret_key();
			String symbol = vo.getSymbol();
			String order_id = vo.getOrder_id();
			String res = iStockRestApi.order_info(api_key, secret_key, symbol, order_id);
			JSONObject resultObject = JSONObject.parseObject(res);
			resultForm = ResultForm.createSuccessResultForm(resultObject, "成功");
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage(), e);
			resultForm = ResultForm.createErrorResultForm(null, e.getMessage());
		}
		return resultForm;
	}

	@ApiOperation(value = "批量获取用户订单")
	@RequestMapping(value = "/getOrdersInfo", method = RequestMethod.POST)
	public ResultForm<?> getOrdersInfo(@RequestBody @Valid OkOrdersInfoRequestVO vo) {
		ResultForm<?> resultForm = null;
		try {
			String api_key = vo.getApi_key();
			String secret_key = vo.getSecret_key();
			String type = vo.getType();
			String symbol = vo.getSymbol();
			String order_id = vo.getOrder_id();
			String res = iStockRestApi.orders_info(api_key, secret_key, type, symbol, order_id);
			JSONObject resultObject = JSONObject.parseObject(res);
			resultForm = ResultForm.createSuccessResultForm(resultObject, "成功");
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage(), e);
			resultForm = ResultForm.createErrorResultForm(null, e.getMessage());
		}
		return resultForm;
	}

	@ApiOperation(value = "获取历史订单信息", notes = "只返回最近七天的信息")
	@RequestMapping(value = "/getOrderHistory", method = RequestMethod.POST)
	public ResultForm<?> getOrderHistory(@RequestBody @Valid OkOrderHistoryRequestVO vo) {
		ResultForm<?> resultForm = null;
		try {
			String api_key = vo.getApi_key();
			String secret_key = vo.getSecret_key();
			String symbol = vo.getSymbol();
			String status = vo.getStatus();
			String current_page = vo.getCurrent_page();
			String page_length = vo.getPage_length();
			String res = iStockRestApi.order_history(api_key, secret_key, symbol, status, current_page, page_length);
			JSONObject resultObject = JSONObject.parseObject(res);
			resultForm = ResultForm.createSuccessResultForm(resultObject, "成功");
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage(), e);
			resultForm = ResultForm.createErrorResultForm(null, e.getMessage());
		}
		return resultForm;
	}
/*
	@ApiOperation(value = "期货行情")
	@RequestMapping(value = "/futureTicker", method = RequestMethod.POST)
	public ResultForm<?> futureTicker(@RequestBody @Valid OkFutureTickerVO vo) {
		ResultForm<?> resultForm = null;
		try {
			String res = iFutureRestApi.future_ticker(vo.getSymbol(), vo.getContract_type());
			JSONObject resultObject = JSONObject.parseObject(res);
			resultForm = ResultForm.createSuccessResultForm(resultObject, "成功");
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage(), e);
			resultForm = ResultForm.createErrorResultForm(null, e.getMessage());
		}
		return resultForm;
	}

	@ApiOperation(value = "期货指数")
	@RequestMapping(value = "/futureIndex", method = RequestMethod.POST)
	public ResultForm<?> futureIndex(@RequestBody String symbol) {
		ResultForm<?> resultForm = null;
		try {
			String res = iFutureRestApi.future_index(symbol);
			JSONObject resultObject = JSONObject.parseObject(res);
			resultForm = ResultForm.createSuccessResultForm(resultObject, "成功");
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage(), e);
			resultForm = ResultForm.createErrorResultForm(null, e.getMessage());
		}
		return resultForm;
	}

	@ApiOperation(value = "期货交易记录")
	@RequestMapping(value = "/futureTrades", method = RequestMethod.POST)
	public ResultForm<?> futureTrades(@RequestBody @Valid OkFutureTradesVO vo) {
		ResultForm<?> resultForm = null;
		try {
			String res = iFutureRestApi.future_trades(vo.getSymbol(), vo.getContract_type());
			JSONObject resultObject = JSONObject.parseObject(res);
			resultForm = ResultForm.createSuccessResultForm(resultObject, "成功");
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage(), e);
			resultForm = ResultForm.createErrorResultForm(null, e.getMessage());
		}
		return resultForm;
	}

	@ApiOperation(value = "期货深度")
	@RequestMapping(value = "/futureDepth", method = RequestMethod.POST)
	public ResultForm<?> futureDepth(@RequestBody @Valid OkFutureDepthVO vo) {
		ResultForm<?> resultForm = null;
		try {
			String res = iFutureRestApi.future_depth(vo.getSymbol(), vo.getContract_type());
			JSONObject resultObject = JSONObject.parseObject(res);
			resultForm = ResultForm.createSuccessResultForm(resultObject, "成功");
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage(), e);
			resultForm = ResultForm.createErrorResultForm(null, e.getMessage());
		}
		return resultForm;
	}

	@ApiOperation(value = "汇率查询")
	@RequestMapping(value = "/futureExchangeRate", method = { RequestMethod.GET, RequestMethod.POST })
	public ResultForm<?> futureExchangeRate() {
		ResultForm<?> resultForm = null;
		try {
			String res = iFutureRestApi.exchange_rate();
			JSONObject resultObject = JSONObject.parseObject(res);
			resultForm = ResultForm.createSuccessResultForm(resultObject, "成功");
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage(), e);
			resultForm = ResultForm.createErrorResultForm(null, e.getMessage());
		}
		return resultForm;
	}

	@ApiOperation(value = "取消订单")
	@RequestMapping(value = "/futureCancel", method = RequestMethod.POST)
	public ResultForm<?> futureCancel(@RequestBody @Valid OkFutureCancelVO vo) {
		ResultForm<?> resultForm = null;
		try {
			String res = iFutureRestApi.future_cancel(vo);
			JSONObject resultObject = JSONObject.parseObject(res);
			resultForm = ResultForm.createSuccessResultForm(resultObject, "成功");
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage(), e);
			resultForm = ResultForm.createErrorResultForm(null, e.getMessage());
		}
		return resultForm;
	}

	@ApiOperation(value = "期货下单")
	@RequestMapping(value = "/futureTrade", method = RequestMethod.POST)
	public ResultForm<?> futureTrade(@RequestBody @Valid OkFutureTradeVO vo) {
		ResultForm<?> resultForm = null;
		try {
			String res = iFutureRestApi.future_trade(vo);
			JSONObject resultObject = JSONObject.parseObject(res);
			resultForm = ResultForm.createSuccessResultForm(resultObject, "成功");
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage(), e);
			resultForm = ResultForm.createErrorResultForm(null, e.getMessage());
		}
		return resultForm;
	}

	@ApiOperation(value = "期货账户信息")
	@RequestMapping(value = "/futureUserinfo", method = RequestMethod.POST)
	public ResultForm<?> futureUserinfo(@RequestBody @Valid OkFutureUserinfoVO vo) {
		ResultForm<?> resultForm = null;
		try {
			String res = iFutureRestApi.future_userinfo(vo);
			JSONObject resultObject = JSONObject.parseObject(res);
			resultForm = ResultForm.createSuccessResultForm(resultObject, "成功");
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage(), e);
			resultForm = ResultForm.createErrorResultForm(null, e.getMessage());
		}
		return resultForm;
	}

	@ApiOperation(value = "期货逐仓账户信息")
	@RequestMapping(value = "/futureUserinfo4fix", method = RequestMethod.POST)
	public ResultForm<?> futureUserinfo4fix(@RequestBody @Valid OkFutureUserinfoVO vo) {
		ResultForm<?> resultForm = null;
		try {
			String res = iFutureRestApi.future_userinfo_4fix(vo);
			JSONObject resultObject = JSONObject.parseObject(res);
			resultForm = ResultForm.createSuccessResultForm(resultObject, "成功");
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage(), e);
			resultForm = ResultForm.createErrorResultForm(null, e.getMessage());
		}
		return resultForm;
	}

	@ApiOperation(value = "用户持仓查询")
	@RequestMapping(value = "/futurePosition", method = RequestMethod.POST)
	public ResultForm<?> futurePosition(@RequestBody @Valid OkFuturePositionVO vo) {
		ResultForm<?> resultForm = null;
		try {
			String res = iFutureRestApi.future_position(vo);
			JSONObject resultObject = JSONObject.parseObject(res);
			resultForm = ResultForm.createSuccessResultForm(resultObject, "成功");
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage(), e);
			resultForm = ResultForm.createErrorResultForm(null, e.getMessage());
		}
		return resultForm;
	}

	@ApiOperation(value = "用户逐仓持仓查询")
	@RequestMapping(value = "/futurePosition4fix", method = RequestMethod.POST)
	public ResultForm<?> futurePosition4fix(@RequestBody @Valid OkFuturePositionVO vo) {
		ResultForm<?> resultForm = null;
		try {
			String res = iFutureRestApi.future_position_4fix(vo);
			JSONObject resultObject = JSONObject.parseObject(res);
			resultForm = ResultForm.createSuccessResultForm(resultObject, "成功");
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage(), e);
			resultForm = ResultForm.createErrorResultForm(null, e.getMessage());
		}
		return resultForm;
	}

	@ApiOperation(value = "获取用户订单信息")
	@RequestMapping(value = "/futureOrderInfo", method = RequestMethod.POST)
	public ResultForm<?> futureOrderInfo(@RequestBody @Valid OkFutureOrderInfoVO vo) {
		ResultForm<?> resultForm = null;
		try {
			String res = iFutureRestApi.future_order_info(vo);
			JSONObject resultObject = JSONObject.parseObject(res);
			resultForm = ResultForm.createSuccessResultForm(resultObject, "成功");
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage(), e);
			resultForm = ResultForm.createErrorResultForm(null, e.getMessage());
		}
		return resultForm;
	}
*/
}
